'''
Created on 2010/5/2

@author: Daniel
'''
from parser_boxscore import ParserBoxScore

class BoxScore2:
    """
    http://sports.yahoo.com/mlb/boxscore?gid=%s' % self.gid
    """
    def __init__(self, gid):
        self.gid = gid
    
    def run(self):
        """
        """
        from urllib import urlopen        
        url = "http://sports.yahoo.com/mlb/boxscore?gid=%s" % self.gid 
        p = ParserBoxScore(urlopen(url).read())
        p.run()
        
        def teamBoxScoreToString(teamBoxScore):
            str = "%s%-10s%2s, %s%-10s%2s\n" % (teamBoxScore[0][0][0], 
                                                teamBoxScore[0][0][2], 
                                                teamBoxScore[0][0][1],
                                                teamBoxScore[0][1][0], 
                                                teamBoxScore[0][1][2], 
                                                teamBoxScore[0][1][1])
            for item in teamBoxScore[1:-1]:
                str += ("%-15s" % (item[0]))
                for subitem in item[1:]:
                    str += " %2s" % (subitem)
                str += ("\n")
            for item in teamBoxScore[-1]:
                str += "%2s: %-s%s\n" % item
            return str
        
        def summaryToString(summary):
            """
            """
            str = ""
            for s in summary:
                str += s
                str += "\n"
            return str
        
        def awayFielderBoxScoreToString(awayFielderBoxScore):
            """
            """
            str = ""
            str += '%s\n' % awayFielderBoxScore['Team']            
            str += '%-25s %3s %3s %3s %3s %3s %3s %3s %3s %5s %5s\n' % awayFielderBoxScore['Title']
            for p in awayFielderBoxScore['Player']:
                s = '%s(%s) ' % p[0]
                str += '%-25s ' % s
                str += '%3s %3s %3s %3s %3s %3s %3s %3s %5s   %5s\n' % tuple(p[1:])
            str += '                          %3s %3s %3s %3s %3s %3s %3s %3s %5s \n' % tuple(awayFielderBoxScore['Total'])
            return str
        
        def awayFielderOthersToString(awayFielderBoxOthers):
            """
            """            
            str = ""
            for key, value in awayFielderBoxOthers.iteritems():                
                if len(value) != 0:
                    str += "%s\n" % key
                    for item in value:
                        str += "%s %s\n" % item
                    str += "\n"
            return str
        
        def homeFielderBoxScoreToString(homeFielderBoxScore):
            """
            """
            str = ""
            str += '%s\n' % homeFielderBoxScore['Team']            
            str += '%-25s %3s %3s %3s %3s %3s %3s %3s %3s %5s %5s\n' % homeFielderBoxScore['Title']
            for p in homeFielderBoxScore['Player']:
                s = '%s(%s) ' % p[0]
                str += '%-25s ' % s
                str += '%3s %3s %3s %3s %3s %3s %3s %3s %5s   %5s\n' % tuple(p[1:])
            str += '                          %3s %3s %3s %3s %3s %3s %3s %3s %5s \n' % tuple(homeFielderBoxScore['Total'])
            return str            
        
        def homeFielderOthersToString(homeFielderOthers):
            """
            """            
            str = ""
            for key, value in homeFielderOthers.iteritems():                
                if len(value) != 0:
                    str += "%s\n" % key
                    for item in value:
                        str += "%s %s\n" % item
                    str += "\n"
            return str
        
        def pitcherBoxScoreToString(pitcherBoxScore):
            """
            """
            str = ''
            def _teamPitchBoxScore(node):
                subStr = ''
                subStr += '%s\n' % node[0]
                subStr += '%-25s %3s %3s %3s %5s %3s %3s %3s %5s %s\n' % pitcherBoxScore['Title']
                for p in node[1]:
                    s = ''
                    if len(p[0]) == 2:
                        s = '%s%s' % p[0]
                    else:
                        s = '%s' % p[0]
                    subStr += '%-25s ' % s
                    subStr += '%3s %3s %3s %5s %3s %3s %3s %5s    %s\n' % tuple(p[1:])
                subStr += '\n'
                return subStr
            
            str += _teamPitchBoxScore(pitcherBoxScore['Away'])
            str += _teamPitchBoxScore(pitcherBoxScore['Home'])          
            return str
        
        def pitcherOthersToString(pitcherOthers):
            """
            """
            str = ''
            for item in pitcherOthers:
                str += '%s %s\n' % item    
            return str  
              
        def gameDetailToString(gameDetail):
            """
            """
            str = ''
            for item in gameDetail:
                str += '%s\n' % item
            return str
        
        self.strTeamBoxScore = teamBoxScoreToString(p.teamBoxScore)
        self.strSummary = summaryToString(p.gameBoxScore['Summary'])
        self.strAwayFielderBoxScore = awayFielderBoxScoreToString(p.gameBoxScore['AwayFielderBoxScore'])
        self.strAwayFielderOthers = awayFielderOthersToString(p.gameBoxScore['AwayFielderOthers'])
        self.strHomeFielderBoxScore = homeFielderBoxScoreToString(p.gameBoxScore['HomeFielderBoxScore'])
        self.strHomeFielderOthers = homeFielderOthersToString(p.gameBoxScore['HomeFielderOthers'])
        self.strPitcherBoxScore = pitcherBoxScoreToString(p.gameBoxScore['PitcherBoxScore'])
        self.strPitcherOthers = pitcherOthersToString(p.gameBoxScore['PitcherOthers'])
        self.strGameDetail = gameDetailToString(p.gameBoxScore['GameDetail'])
    
    
    def showBoxScore(self):
        print self.strTeamBoxScore
        print self.strSummary
        print 'Away - ', self.strAwayFielderBoxScore
        print self.strAwayFielderOthers
        print 'Home - ', self.strHomeFielderBoxScore
        print self.strHomeFielderOthers
        print self.strPitcherBoxScore
        print self.strPitcherOthers
        print self.strGameDetail

if __name__ == '__main__':
    b = BoxScore2('300503118')
    b.run()
    b.showBoxScore()